#随机梯度下降
import numpy
from matplotlib import pyplot
from sklearn.datasets import make_moons
data, y = make_moons(n_samples=500, noise=0.05, random_state=42)
import matplotlib.pyplot as plt

# 可视化数据
plt.scatter(data[:, 0], data[:, 1],c=y)
plt.title("Moon-shaped Dataset")
plt.xlabel("Feature 1")
plt.ylabel("Feature 2")
plt.show()
#data=numpy.loadtxt('./testSet.txt',delimiter='\t',encoding='utf-8')
data=numpy.hstack((data,y.reshape(-1,1)))
def line(w,b,x):
    return (-b-w[0]*x)/w[1]
def sigmoid(x):
    return 1/(1+numpy.exp(-x))
def regression(data,w,b,y,epoch,record1,record2,alpha):
    length=len(y)
    for i in range(epoch):
        for j in range(length):
            alpha=0.0001+1/(1+i+j)#逐渐降低学习率防止高频波动，使得线条变得平稳,越逼近好结果，走的越慢
            index=numpy.random.randint(0,length)#随机抽取样本减少线条周期性的波动
            temp = (sigmoid(numpy.dot(data[index],w)+b) - y[index]) * sigmoid(numpy.dot(data[index],w)+b) *\
                    (1 - sigmoid(numpy.dot(data[index],w)+b))
            w_delta=temp*data[index]
            b_delta=temp
            w=w-alpha*w_delta
            b=b-alpha*b_delta
            res=sigmoid(numpy.dot(data,w)+b)
            res=numpy.where(res<0.5,0,1)
            percent=numpy.sum(res==y)/length
            print(percent)
            record1[i*100+j,0]=w[0]
            record1[i*100+j,1]=w[1]
            record2[i*100+j]=b[0]
        # record1[i , 0] = w[0]
        # record1[i , 1] = w[1]
        # record2[i] = b[0]
    return  w,b
shape=data.shape
# w=numpy.random.randn(shape[1]-1)
# b=numpy.random.randn(1)
w=numpy.ones(shape[1]-1)
b=numpy.ones(1)
record1=numpy.empty((50000,2))
record2=numpy.empty(50000)
w,b=regression(data[:,0:-1],w,b,data[:,-1],200,record1,record2,0.1)
pyplot.scatter(data[:,0],data[:,1],c=data[:,2],s=3)
pyplot.plot(list(range(-3,4)),line(w,b,numpy.array(list(range(-3,4)))))
pyplot.show()
pyplot.plot(list(range(1,20001)),record1[:,0],label='w1',linewidth=0.5)
pyplot.xlim([1,20000])
pyplot.legend()
pyplot.show()
pyplot.plot(list(range(1,20001)),record1[:,1],label='w2',linewidth=0.5)
pyplot.xlim([1,20000])
pyplot.legend()
pyplot.show()
pyplot.plot(list(range(1,20001)),record2,label='b',linewidth=0.5)
pyplot.legend()
pyplot.show()